import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  baseUrl = 'http://localhost:8088/jrgw/';
  options = {};
  header = {};

  constructor(private http: HttpClient) { }

  // 发送get请求而
  // 返回集合
  getItems<T>(apiUrl: string, params: any): Observable<T[]> {
    return this.http.get<T[]>(apiUrl, { params: params });
  }
  // 返回单个实体
  getItem<T>(apiUrl: string, params: any): Observable<T> {
    return this.http.get<T>(apiUrl, { params: params });
  }
  sendGetRequest(api: string, params: any, callback?: (res: any, error: any) => void): void {
    this.http.get(api, { params: params }).subscribe(res => {
      callback(res, null);
    }, error => {
      callback(null, error);
    });
  }

  // 发送post请求
  post(api: string, params: any, callback?: (res: any, error: any) => void): void {
    this.sendPostRequest(this.baseUrl + api, params, callback);
  }
  sendPostRequest(api: string, params: any, callback?: (res: any, error: any) => void): void {
    this.http.post(api, null, { params: params }).subscribe(res => {
      callback(res, null);
    }, error => {
      callback(null, error);
    });
  }
}
